package com.zy.controller;

import cn.hutool.json.JSONUtil;
import com.zy.ai.service.TestService;
import com.zy.ai.service.WordService;
import com.zy.word.WordContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @BelongsProject: SmartBidAI
 * @BelongsPackage: com.zy.controller
 * @Author: zhangyan
 * @CreateTime: 2025-09-01  20:42
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private TestService testService;
    @Autowired
    private WordService wordService;

    @RequestMapping("/normal")
    public WordContent test(String ai970923) throws IOException {
        WordContent chunkResult = wordService.parseWord(ai970923);

        return chunkResult;
    }
    // 合并多个 WordContent
    private void mergeWordContent(WordContent main, WordContent chunk) {
        if (chunk.getOrgName() != null) main.setOrgName(chunk.getOrgName());
        if (chunk.getOpenCount() != null) main.setOpenCount(chunk.getOpenCount());
        if (chunk.getEvalMethodName() != null) main.setEvalMethodName(chunk.getEvalMethodName());
        if (chunk.getProcessInstructions() != null) main.setProcessInstructions(chunk.getProcessInstructions());
        if (chunk.getClassifyMap() != null) {
            if (main.getClassifyMap() == null) main.setClassifyMap(chunk.getClassifyMap());
            else main.getClassifyMap().putAll(chunk.getClassifyMap());
        }
        if (chunk.getInventoryList() != null) {
            if (main.getInventoryList() == null) main.setInventoryList(chunk.getInventoryList());
            else main.getInventoryList().addAll(chunk.getInventoryList());
        }
        if (chunk.getUploadFileNameList() != null) {
            if (main.getUploadFileNameList() == null) main.setUploadFileNameList(chunk.getUploadFileNameList());
            else main.getUploadFileNameList().addAll(chunk.getUploadFileNameList());
        }
        if (chunk.getOpenParameterList() != null) {
            if (main.getOpenParameterList() == null) main.setOpenParameterList(chunk.getOpenParameterList());
            else main.getOpenParameterList().addAll(chunk.getOpenParameterList());
        }
        if (chunk.getEvalProcessList() != null) {
            if (main.getEvalProcessList() == null) main.setEvalProcessList(chunk.getEvalProcessList());
            else main.getEvalProcessList().addAll(chunk.getEvalProcessList());
        }
        if (chunk.getAlgorithmDescription() != null) main.setAlgorithmDescription(chunk.getAlgorithmDescription());
    }
    @GetMapping(value = "/stream-flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> streamChat(@RequestParam String question, @RequestParam int memoryId) {
        Flux<String> contentFlux = testService.questFlux(memoryId, question);
        // 调用服务生成代码（SSE 流式返回）
        return contentFlux
                .map(chunk -> {
                    Map<String, String> wrapper = Map.of("d", chunk);
                    String jsonData = JSONUtil.toJsonStr(wrapper);
                    return ServerSentEvent.<String>builder()
                            .data(jsonData)
                            .build();
                })
                .concatWith(Mono.just(
                        // 发送结束事件
                        ServerSentEvent.<String>builder()
                                .event("done")
                                .data("")
                                .build()
                ));
    }

}